import { DatabaseHelper } from './DatabaseHelper.js';

let myHandler = async function (event, context, callback, logger) {
  logger.info(JSON.stringify(event))
  let userid: string = event.body ? JSON.parse(event.body).userId : event.userId;
  let followTopics: string = event.body ? JSON.parse(event.body).followTopics : event.followTopics;
  let msg;
  let resInsert;
  let resDelete;
  try {
    const databaseHelper = new DatabaseHelper(context, logger);
    if (followTopics&&followTopics.length > 0) {
      resInsert = await databaseHelper.modifyTopic(followTopics, userid);
    }
    if (resInsert || resDelete) {
      callback({
        code: 0,
        message: 'Success',
        data: null,
      });
    } else {
      callback({
        code: 1,
        message: 'Error',
        data: null,
      });
    }
  } catch (err) {
    logger.error("func error:" + err.message + " stack:" + err.stack);
    callback({
      code: 3,
      message: '数据操作失败',
      data: err,
    });
  }
}


export { myHandler };